package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.enums.ProductTypeEnum;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.sys.bean.SysProduct;
import com.fzrskj.cloud.free.sys.bean.SysPromotion;
import com.fzrskj.cloud.free.sys.dao.SysProductDao;
import com.fzrskj.cloud.free.sys.dao.SysPromotionDao;
import com.fzrskj.cloud.free.user.bean.UserDiyBean;
import com.fzrskj.cloud.free.user.utils.UserDiyUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.StrTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysPromotionUtils extends CObject{
	
	//删除产品促销
	public int delSysPromotion(Map<String, Object> cdt){
		SysPromotionDao dao=new SysPromotionDao();
		int i = dao.delSysPromotion(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return i;
	}

	//添加产品促销
	public boolean addSysPromotion(String priceItem){
		JSONArray json = JSONArray.fromObject(priceItem);
		if(json.size()<=0) {
			return false;
		}

		for(int i=0;i<json.size();i++){
			JSONObject job = json.getJSONObject(i);
			SysPromotion sp=new SysPromotion();
			sp.setProductCode(job.getString("productCode"));
			sp.setChargeId(StrTool.strToIntDef(job.getString("chargeId"), 0));
			sp.setApplyTime(StrTool.strToIntDef(job.getString("applyTime"), 0));
			sp.setApplyType(job.getString("applyType"));
			sp.setSaleType(StrTool.strToIntDef(job.getString("saleType"), 0));
			sp.setSaleValue(job.getString("saleValue"));
			if (job.containsKey("plusValue1")) {
				sp.setPlusValue1(job.getString("plusValue1"));
			}
			if (job.containsKey("plusValue2")) {
				sp.setPlusValue2(job.getString("plusValue2"));
			}
			SysPromotionDao dao=new SysPromotionDao();
			dao.addSysPromotion(sp);
			dao.dispose();
		}

		return true;
	}

	//获取产品促销列表
	public List<SysPromotion> getSysPromotionList(String productCode, int id, int chargeId){
		SysPromotionDao dao = new SysPromotionDao();
		SysProductDao sysProductDao = new SysProductDao();
		UserDiyUtils diyUtils = new UserDiyUtils();
		SysDiyProductPriceUtils sysDiyProductPriceUtils = new SysDiyProductPriceUtils();

		List<SysPromotion> list = new ArrayList<>();
		Map<String, Object> cdt = new HashMap<>();

		do {
			SysProduct sysProduct =  sysProductDao.queryProductByProductCode(productCode);

			if (null == sysProduct){
				this.setErrCode(sysProductDao.getErrCode());
				break;
			}

			String productType = sysProduct.getProductClass();
			if(ProductTypeEnum.DIY.getTag().equals(productType)){
				list = diyUtils.getDiyHostRenewPriceTime(id);
			}
			if (list.size() > 0){
				this.setErrCode(SysErr.CODE_SUCCESS);
				break;
			}

			cdt.put("productCode", productCode);
			cdt.put("chargeId", chargeId);
			cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);

			//todo 添加提示特殊处理
			JSONObject productParam = JSONObject.fromObject(sysProduct.getProductParam());
			int priceSaleType = null != productParam && productParam.containsKey("saleType") ? productParam.getInt("saleType") : 1;
			if (priceSaleType == 2) {
				if(ProductTypeEnum.DIY.getTag().equals(productType)){
					UserDiyBean diy = diyUtils.qryUserDiy(id);
					if (null != diy){
						cdt.put("userId", diy.getUserId());
					}
				}
				cdt.put("productClass", productType);
				list = sysDiyProductPriceUtils.getDiyProductBuyTime(cdt);
				this.setErrCode(sysDiyProductPriceUtils.getErrCode());
				if (list.size() > 0) {
					this.setErrCode(SysErr.CODE_SUCCESS);
					break;
				}
			}

			if (cdt.containsKey("userId")) {
				cdt.remove("userId");
			}
			if (cdt.containsKey("productClass")) {
				cdt.remove("productClass");
			}

			list = dao.getSysPromotionList(cdt);
			String code = dao.getErrCode();
			this.setErrCode(code);
		}while (false);
		diyUtils.dispose();
		sysProductDao.dispose();
		dao.dispose();
		sysDiyProductPriceUtils.dispose();

		return list;
	}

	//获取产品促销列表
	public List<SysPromotion> getSysPromotionList(Map<String, Object> cdt){
		SysPromotionDao dao = new SysPromotionDao();
		List<SysPromotion> list = dao.getSysPromotionList(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 查询促销消息.
	 * @param cdt 查询条件.
	 * @return 促销消息.
	 */
	public SysPromotion queryPromotion(Map<String, Object> cdt){
		SysPromotionDao dao=new SysPromotionDao();
		SysPromotion obj=dao.queryPromotion(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return obj;
	}

	/**
	 * 指定代理商查询促销消息.
	 * @param cdt 查询条件.
	 * @param agentId 代理商ID.
	 * @return 促销消息.
	 */
	public SysPromotion queryPromotionAppointAgentId(Map<String, Object> cdt, Integer agentId) {
		SysPromotionDao dao=new SysPromotionDao();
		SysPromotion obj=dao.queryPromotionAppointAgentId(cdt, agentId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return obj;
	}
}
